#! /usr/bin/env python
#coding=utf-8

import os,wx
import model


css = model.css()
js  = model.js()
html  = model.html()
png = model.png()
other  = model.other()

print '''
    Static Resources Packaged Tools
    Author chenwp <chenwp@5173.com>
    Version v0.1
    
    Runing.........    
'''

class MyFrame(wx.Frame):
    
    def __init__(self):
        wx.Frame.__init__(self,None,-1,u'前端开发打包发布工具', size=(750,550))
        pannel = wx.Panel(self,-1)
        wx.StaticText(pannel, -1, u'路径:', pos=(10,12))
        wx.StaticText(pannel, -1, u'选择要打包的目录', pos=(385,12))
        wx.StaticText(pannel, -1, u'打包过程中可能会出现假死，请等待一会。', pos=(165,45))
        wx.StaticText(pannel, -1, u'结果:', pos=(10,80))
        statusBar = self.CreateStatusBar()
        self.baseCtrl = wx.TextCtrl(pannel,-1,'',pos=(60,10),size=(275,20))
        self.btnOK = wx.Button(pannel,-1,u'开始打包',pos=(60,40),size=(90,25))        
        self.btnSL = wx.Button(pannel,-1,u'选择',pos=(340,10),size=(40,20))        
        self.btnOK.Bind(wx.EVT_BUTTON,self.onclick1)
        self.btnSL.Bind(wx.EVT_BUTTON,self.onclick2)
        self.t = wx.TextCtrl(pannel, pos=(60,80), size=(640, 390), style=wx.TE_MULTILINE)     
    
    #选择目录    
    def onclick2(self,event):
        dlg = wx.DirDialog(self, message = u'请先选择目录', style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
        if dlg.ShowModal() == wx.ID_OK:  
            self.baseCtrl.SetValue(dlg.GetPath())
            dlg.Destroy()
    
    #优化按钮   
    def onclick1(self,event):
        dir = self.baseCtrl.GetValue()
        if dir=='':
            self.SetStatusText(u'请选择目录')
            return False
        file_in_folder(self, dir)
        self.SetStatusText(u'完成')

def file_in_folder(self,dirname):  
    tmp = ''
    for root, dirs, files in os.walk(dirname):
        for file1 in files:    
            if root.find('src') > 0 : 
                
                oldfile = root + os.sep + file1
                newfile = oldfile.replace('src','build')
                
                if file1.endswith('.html') or file1.endswith('.htm') or file1.endswith('.php') or file1.endswith('.shtml') :
                    html.process(root, file1)  
                    tmp = tmp + (newfile + '\r\n')                    
                    pass
                
                if file1.endswith('.css'):
                    css.hbcss(root, file1)
                    tmp = tmp + (newfile + '\r\n')                  
                    pass
                
                if file1.endswith('.js'):
                    js.hbjs(root,file1)
                    tmp = tmp + (newfile + '\r\n')                  
                    pass
                
                if file1.endswith('.png'):
                    png.Optimizer(root,file1)
                    tmp = tmp + (newfile + '\r\n')               
                    pass

                if file1.endswith('.jpg') or file1.endswith('.gif') or file1.endswith('.swf') or file1.endswith('.flv') or file1.endswith('.htc') :
                    other.cp(root,file1)
                    tmp = tmp + (newfile + '\r\n')               
                    pass

            self.t.SetValue(tmp)

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = MyFrame()
    frame.Show(True)
    app.MainLoop()